package com.xh.web.controller;

import java.sql.Connection;
import java.util.List;

import com.xh.web.constant.AjaxResult;
import com.xh.web.domain.DDbConnect;
import com.xh.web.domain.vo.SyncDbVo;
import com.xh.web.service.IDDbConnectService;
import com.xh.web.util.DbUtil;
import com.xh.web.util.HttpClientUtil;
import com.xh.web.util.JsonUtil;
import com.xh.web.util.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * 业务名Controller
 *
 * @author 宁兴海
 * @date 2022-07-27
 */
@RestController
@RequestMapping("/db/conn")
public class DDbConnectController extends BaseController
{
    @Autowired
    private IDDbConnectService dDbConnectService;

    /**
     * 查询业务名列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DDbConnect dDbConnect)
    {
        startPage();
        List<DDbConnect> list = dDbConnectService.selectDDbConnectList(dDbConnect);
        return getDataTable(list);
    }

    /**
     * 获取业务名详细信息
     */
    /*@GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(dDbConnectService.selectDDbConnectById(id));
    }*/

    /**
     * 新增业务名
     */
    @PostMapping(value = "/add")
    public AjaxResult add(@RequestBody DDbConnect dDbConnect)
    {
        return toAjax(dDbConnectService.insertDDbConnect(dDbConnect));
    }

    /**
     * 修改业务名
     */
    @PutMapping(value = "/edit")
    public AjaxResult edit(@RequestBody DDbConnect dDbConnect)
    {
        return toAjax(dDbConnectService.updateDDbConnect(dDbConnect));
    }

    /**
     * 删除业务名
     */
	@DeleteMapping("/remove/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(dDbConnectService.deleteDDbConnectByIds(ids));
    }

    @PostMapping(value = "/testconn")
    public AjaxResult testconn(@RequestBody DDbConnect dDbConnect){
        try {
            Connection conn=DbUtil.getConn(dDbConnect);
            conn.close();
            conn=null;
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("连接失败,请确认数据库连接信息是否正确");
        }
        return AjaxResult.success("连接成功");
    }

    @GetMapping(value = "/inport")
    public AjaxResult inport(String connId,String projectId){
        try {
            dDbConnectService.connInport(connId,projectId);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败");
        }
        return AjaxResult.success();
    }

    @PostMapping(value = "/sync")
    public AjaxResult sync(@RequestBody SyncDbVo syncDbVo){

        if(syncDbVo.getSyncType().equals("1")){
            return localToPlat(syncDbVo);
        }
        return AjaxResult.error("平台同步到本地还在开发中");
    }

    private AjaxResult localToPlat(SyncDbVo syncDbVo){
        try {
            //需要传的表和列 集合
            SyncDbVo syncDbVo1 = dDbConnectService.getLocalDbInfoById(syncDbVo.getProjectId(),syncDbVo.getTableCode());

            syncDbVo1.setOrgCode(syncDbVo.getOrgCode());
            syncDbVo1.setUserName(syncDbVo.getUserName());
            syncDbVo1.setPassword(syncDbVo.getPassword());
            syncDbVo1.setProjectCode(syncDbVo.getProjectCode());
            String toJSONString = JsonUtil.toJSONString(syncDbVo1);
            System.out.println(toJSONString);
            String respJson = HttpClientUtil.httpPost("http://101.200.59.11/prod-api/db/project/import", null, toJSONString);
            //String respJson = HttpClientUtil.httpPost("http://localhost:8080/db/project/import", null, toJSONString);
            return JsonUtil.parseObject(respJson,AjaxResult.class);
        }catch (Exception e){
            return AjaxResult.error("网络连接失败");
        }
    }
}
